home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12932 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: beginner question
  5. Date: Wed, 03 Apr 96 14:22:41 GMT
  6. Organization: none
  7. Message-ID: <828541361snz@genesis.demon.co.uk>
  8. References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com> <315F525A.4A1CD496@alcyone.com> <31625C30.12AA@sooner.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <31625C30.12AA@sooner.net> edwbush@sooner.net "Eddie Bush" writes:
  15.  
  16. >Erik Max Francis wrote:
  17. >> 
  18. >> Tarang Deshpande wrote:
  19. >> 
  20. >> > So then what does the following mean:
  21. >> >
  22. >> > struct _FOO
  23. >> > {
  24. >> >         int     bar;
  25. >> > } FOO;
  26. >> >
  27. >> > struct _FOO s1;
  28. >> > FOO         s2;
  29. >> >
  30. >> > Why?
  31. >> 
  32. >> This is a compiler error.
  33. >
  34. >I'm sorry.  I don't see an error -- just two different variables of the same
  35. > type that happen to 
  36. >be declared using two different (and correct) methods.
  37.  
  38. FOO here is defined as a variable, not a type since there was no typedef.
  39. It is just as illegal as writing:
  40.  
  41.       int BAR;
  42.       BAR y;
  43.  
  44. you must write it as:
  45.  
  46.       typedef int BAR;
  47.       BAR y;
  48.  
  49. >>     struct _FOO { int bar; } FOO;
  50. >> 
  51. >> declares a structure called struct _FOO and an instance of that structure
  52. > FOO.
  53. >> The further declaration
  54. >
  55. >The initial declaration of the structure doesn't make available FOO for
  56. > manipulation.  FOO is a 
  57. >type.  ...isn't it?
  58.  
  59. As written FOO is defined as a variable, not a type.
  60.  
  61. > I always understood that 'struct _FOO' is the 'tag name'
  62. > (the name of the 
  63. >structure', and that FOO would be a type which you have defined (by the typedef
  64. > struct _FOO) to 
  65. >be the structure _FOO.
  66.  
  67. If there was a typedef there, that would be true.
  68.  
  69. ...
  70.  
  71. >Also, from what I understand, you would do it like this:
  72. >
  73. >typedef struct {
  74. >        int num;
  75. >} FOOBAR;
  76. >
  77. >This would be if you intended to use the type statically.  Or, you could:
  78. >
  79. >typedef struct FOO {
  80. >        int num;
  81. >} *BAR;
  82. >
  83. >if you happened to want to allocate the variable dynamically.
  84.  
  85. This defines BAR type as a pointer to a struct FOO. You can do with it what
  86. you can do with other pointers, e.g. make a variable of that type point
  87. to an object (or one of an array of objects) created by malloc.
  88.  
  89. >Notice, that I left the 'tag name' off of the variable which I intend to use
  90. > statically.  That is 
  91. >because it is not needed -- you only need it to allocate memory dynamically:
  92.  
  93. Be clear that it isn't the variable of type BAR that is in any sense 'dynamic',
  94. it is simply a pointer. However it can point to objects allocated dynamically.
  95.  
  96. >BAR new_bar;
  97. >
  98. >new_bar = (BAR) malloc (sizeof (struct FOO));
  99.  
  100. Hiding pointers behind typedefs generally confuses unless the 'pointerness'
  101. of the type is not important (which it is here). However sticking with
  102. BAR here this is probably best written as:
  103.  
  104.   new_bar = malloc (sizeof *new_bar);
  105.  
  106. -- 
  107. -----------------------------------------
  108. Lawrence Kirby | fred@genesis.demon.co.uk
  109. Wilts, England | 70734.126@compuserve.com
  110. -----------------------------------------
  111.